
#import "GestureWindow.h"
 

#define MIN_SWIPE_DISTANCE 100.0f
#define MAX_VERTICAL_SWIPE_DISTANCE 48.0f



@implementation GestureWindow

@synthesize previousTouchView;
@synthesize previousTouchLocation;
@synthesize notificationCenter;

- (void) dealloc
{
	[previousTouchView release];
	[notificationCenter release];
	[super dealloc];
}


+(TouchHandler*)getHandler {
	
	if ( !mainTouchHandler ) { 
		mainTouchHandler = [[TouchHandler alloc] init];
	}
	
	return mainTouchHandler;
	
}




- (id) initWithCoder:(NSCoder*)coder
{    
    if ((self = [super initWithCoder:coder]))
	{
		
		
		//touchHandler = [[TouchHandler alloc] init];
		//touchHandler.parentView = self;
		
		if ( !mainTouchHandler ) { 
			mainTouchHandler = [[TouchHandler alloc] init];
		}
		
		mainTouchHandler.parentView = self;
		
	}
	
	return self;
}


- (void)sendEvent:(UIEvent *)event {
	
	/*
	NSSet *touches = [event allTouches];
	
	for ( UITouch * touch in touches ) {
		
		if(touch.phase == UITouchPhaseBegan) {
			
		} else if(touch.phase == UITouchPhaseBegan) { 
			
		} else if(touch.phase == UITouchPhaseBegan) {
			
		}
		
	}
	*/
	
	[super sendEvent:event];
	
	
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{	
	
	[mainTouchHandler touchesBegan:touches withEvent:event];
	[super touchesBegan:touches withEvent:event];
	
	//NSLog(@"BEGAINNN");
	
}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	[mainTouchHandler touchesMoved:touches withEvent:event];
	[super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	
	[mainTouchHandler touchesEnded:touches withEvent:event];
	[super touchesEnded:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
	
	[mainTouchHandler touchesCancelled:touches withEvent:event];
	[super touchesCancelled:touches withEvent:event];
}



- (void)sendEventSWIPING:(UIEvent *)event {
	
	
	NSSet *touches = [event allTouches];
	if([touches count] == 1) {
		UITouch *touch = [touches anyObject];
		CGPoint location = [touch locationInView:touch.view];
		if(touch.phase == UITouchPhaseBegan) {
			//save new single touch
			self.previousTouchView = touch.view;
			self.previousTouchLocation = [touch locationInView:touch.view];
		}
		else if (touch.phase == UITouchPhaseEnded) {
			if([touch.view isEqual:self.previousTouchView] && fabsf(self.previousTouchLocation.y - location.y) <= MAX_VERTICAL_SWIPE_DISTANCE) {
				//evaluate gesture
				if(self.previousTouchLocation.x < location.x && location.x - self.previousTouchLocation.x > MIN_SWIPE_DISTANCE) {
					
					NSLog(@"Swipe Right");
					
					//left to right
					[self.notificationCenter postNotificationName:@"swipe right" object:self userInfo:[NSDictionary dictionaryWithObject:touch.view forKey:@"view"]];
				}
				else if (self.previousTouchLocation.x > location.x && self.previousTouchLocation.x - location.x > MIN_SWIPE_DISTANCE) {
					
					NSLog(@"Swipe Left");
					//right to left
					[self.notificationCenter postNotificationName:@"swipe left" object:self userInfo:[NSDictionary dictionaryWithObject:touch.view forKey:@"view"]];
				}
			}
			self.previousTouchView = nil;
		}
		else if (touch.phase == UITouchPhaseMoved) {
			if([touch.view isEqual:self.previousTouchView] == NO || fabsf(self.previousTouchLocation.y - location.y) > MAX_VERTICAL_SWIPE_DISTANCE) {
				self.previousTouchView = nil;
			}
		}
		else if (touch.phase == UITouchPhaseCancelled) {
			self.previousTouchView = nil;
		}
	}
	else {
		self.previousTouchView = nil;
	}
	[super sendEvent:event];
}

@end